package com.xjl.everyinterview.tools;

import org.apache.poi.ss.usermodel.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 还有下面两个网站
 * http://www.jsons.cn/json2java
 * https://www.bejson.com/json2javapojo/new/
 * 为写接口文文档设计的工具类
 * 生成是实体代码  摸鱼神器
 *
 * @author xjl
 * @version 1.0
 * @description: TODO
 * @date 2021/8/28 9:45
 */
public class DocSplit {
    public static void main(String[] args) throws FileNotFoundException {

        InputStream inputStream = null;

        String downloadFilePath = "F:\\test.xlsx";
        System.out.println("本地文件服务器：文件路径==> " + downloadFilePath);
        File file = new File(downloadFilePath);

        if (file.exists()) {
            inputStream = new BufferedInputStream(new FileInputStream(file));
        }
        try {
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            // 读取数据
            List<Map<String, Object>> dataMaps = new ArrayList<>();
            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell cell = row.getCell(0);
                cell.setCellType(CellType.STRING);
                String number = cell.getRichStringCellValue().getString();
                DocSplitStringEnetity(i, sheet, "_");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *请求渠道请求渠道：1 PC支付,2 APP,3 H5,5：微信小程序
     */
//            @JSONField(name = "PAY_REQ_WAY")
//            private String payReqWay;

    /**
     * 根据行生成实体代码
     *
     * @param num     行号
     * @param sheet   全部内容
     * @param partten 分割模式
     */
    private static void DocSplitString(int num, Sheet sheet, String partten) {
        Row row = sheet.getRow(num);
        Cell cell = row.getCell(0);
        Cell cell1 = row.getCell(1);
        Cell cell2 = row.getCell(2);
        cell.setCellType(CellType.STRING);
        cell1.setCellType(CellType.STRING);
//        cell2.setCellType(CellType.STRING);
        String number = cell.getRichStringCellValue().getString().trim();
        String number1 = cell1.getRichStringCellValue().getString().trim();
//        String number2 = cell2.getRichStringCellValue().getString();

        if ("".equals(number) || Objects.isNull(number) || "".equals(number1) || Objects.isNull(number1)) {
            return;
        }
        System.out.println("\n");
        System.out.println("            /**\n" +
                "             *" + number + "\n" +
                "             */");

        System.out.println("            @JSONField(name = \"" + number1 + "\")");
        String[] split = number1.split(partten);
        String annotationStr = "";
        for (int i = 0; i < split.length; i++) {
            String son = split[i];
            if (i == 0) {
                annotationStr = son.toLowerCase();
            } else {
                annotationStr = annotationStr + son.substring(0, 1) + son.substring(1, son.length()).toLowerCase();
            }
        }
        System.out.println("            private String " + annotationStr + ";");
    }


    /**
     * 根据行生成实体代码
     *
     * @param num     行号
     * @param sheet   全部内容
     * @param partten 分割模式
     */
    private static void DocSplitStringEnetity(int num, Sheet sheet, String partten) {
        Row row = sheet.getRow(num);
        Cell cell = row.getCell(0);
        Cell cell1 = row.getCell(1);
        Cell cell2 = row.getCell(2);
        cell.setCellType(CellType.STRING);
        cell1.setCellType(CellType.STRING);
//        cell2.setCellType(CellType.STRING);
        String number = cell.getRichStringCellValue().getString().trim();
        String number1 = cell1.getRichStringCellValue().getString().trim();
//        String number2 = cell2.getRichStringCellValue().getString();

        if ("".equals(number) || Objects.isNull(number) || "".equals(number1) || Objects.isNull(number1)) {
            return;
        }
        System.out.println("\n");
        System.out.println("            /**\n" +
                "             *" + number + "\n" +
                "             */");

        System.out.println("            @JSONField(name = \"" + number1 + "\")");
        String[] split = number1.split(partten);
        String annotationStr = "";
        String annotationStrEntity = "";
        for (int i = 0; i < split.length; i++) {
            String son = split[i];
            if (i == 0) {
                annotationStr = son.toLowerCase();
            } else {
                annotationStr = annotationStr + son.substring(0, 1) + son.substring(1, son.length()).toLowerCase();
            }
            annotationStrEntity = annotationStrEntity + son.substring(0, 1).toUpperCase() + son.substring(1, son.length()).toLowerCase();
        }
        System.out.println("            private " + annotationStrEntity + " " + annotationStr + ";");
    }

}
